package com.yanqu.road.server.manger;

import com.yanqu.road.entity.beautyescort.EscortServerGroup;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.servergroup.ICrossServerGroup;
import com.yanqu.road.logic.bussiness.servercenter.ServerBussiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class EscortServerGroupMgr extends TempMgr {

    private static final Logger logger = LogManager.getLogger(EscortServerGroupMgr.class.getName());

    private static Map<Integer, Map<Integer, EscortServerGroup>> channelServerGroupMap;

    private static Map<Integer,EscortServerGroup> serverGroupMap;

    private static ManagerReloadListener managerReloadListener;

    private static ICrossServerGroupAccess crossServerGroupAccess;

    @Override
    public boolean reload() throws Exception {
        reloadServerGroup();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadServerGroup(){
        Map<Integer, Map<Integer, EscortServerGroup>> tempChannelServerGroupMap = new ConcurrentHashMap<>();
        Map<Integer, EscortServerGroup> tempServerGroupMap = new ConcurrentHashMap<>();
        ServerBussiness.getEscortCrossServerGroupMap(tempChannelServerGroupMap, tempServerGroupMap);
        channelServerGroupMap = tempChannelServerGroupMap;
        serverGroupMap = tempServerGroupMap;
        notifyReload();
    }

    private static void notifyReload(){
        if(managerReloadListener != null){
            managerReloadListener.notifyReload();
        }
    }

    public static void addReloadListener(ManagerReloadListener reloadListener){
        managerReloadListener = reloadListener;
    }

    public static Map<Integer, EscortServerGroup> getServerGroupMap(int channelId){
        return channelServerGroupMap.get(channelId);
    }

    public static List<EscortServerGroup> getServerGroupList(int channelId){
        Map<Integer, EscortServerGroup> dataMap = getServerGroupMap(channelId);
        if(null != dataMap){
            return new ArrayList<>(dataMap.values());
        }
        return new ArrayList<>();
    }

    public static EscortServerGroup getServerGroup(int groupId){
        return serverGroupMap.get(groupId);
    }

    public static int getGroupIdByServerId(long serverId){
        int groupId = (int)serverId;
        EscortServerGroup serverGroup = getServerGroupByServerId(serverId);
        if(null != serverGroup){
            groupId = serverGroup.getGroupId();
        }
        return groupId;
    }

    public static EscortServerGroup getServerGroupByServerId(long serverId){
        EscortServerGroup serverGroup = null;
        ServerInfo serverInfo = ServerListMgr.getServerInfo(serverId);
        if(null == serverInfo){
            for(EscortServerGroup tempGroup : serverGroupMap.values()){
                if(tempGroup.getServerList().contains(serverId)){
                    serverGroup = tempGroup;
                    break;
                }
            }
        }else {
            Map<Integer, EscortServerGroup> dataMap = getServerGroupMap(serverInfo.getChannelId());
            if(null != dataMap){
                for(EscortServerGroup tempGroup : dataMap.values()){
                    if(tempGroup.getServerList().contains(serverId)){
                        serverGroup = tempGroup;
                        break;
                    }
                }
            }
        }
        return serverGroup;
    }

    public static Set<Long> getGroupServer(int groupId){
        Set<Long> serverSet = new HashSet<>();
        EscortServerGroup serverGroup = getServerGroup(groupId);
        if(serverGroup == null || serverGroup.getEscortState() == 0){
            serverSet.add((long)groupId);
        }else{
            serverSet.addAll(serverGroup.getServerList());
        }
        return serverSet;
    }

    public static ICrossServerGroupAccess initServerGroupAccess(){

        crossServerGroupAccess = new ICrossServerGroupAccess() {
            @Override
            public int getGroupIdByServerId(long serverId) {
                return EscortServerGroupMgr.getGroupIdByServerId(serverId);
            }

            @Override
            public ICrossServerGroup getServerGroup(int groupId) {
                return EscortServerGroupMgr.getServerGroup(groupId);
            }

            @Override
            public ICrossServerGroup getServerGroupByServerId(long serverId) {
                return EscortServerGroupMgr.getServerGroupByServerId(serverId);
            }

            @Override
            public Set<Long> getGroupServer(int groupId) {
                return EscortServerGroupMgr.getGroupServer(groupId);
            }

            @Override
            public void reloadServerGroup() {
                EscortServerGroupMgr.reloadServerGroup();
            }

        };

        return crossServerGroupAccess;
    }

}
